﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Data;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HotelManager.ServiceReference1;
using System.Collections.Generic;
using HotelManager.ViewModels;
using SilverlightStudentManager.ViewModels;
using System.Windows.Media.Imaging;
using System.IO;
using System.Collections.ObjectModel;
using HotelManager.View;

namespace HotelManager.ViewModels
{
    public class NhanVienViewModel : ViewModelBase
    {
        #region variable
        private NhanVienClient nhanvienclient = new NhanVienClient();
        private BacLuongClient bacluongclient = new BacLuongClient();
        private ChucVuClient chucvuclient = new ChucVuClient();
        private LoaiTaiKhoanClient loaitaikhoanclient = new LoaiTaiKhoanClient();
        private PhongBanClient phongbanclient = new PhongBanClient();
        public PagedCollectionView collection { get; set; }
        private NHANVIEN newNhanVien = new NHANVIEN();
        public ICommand CommandDatagrid { get; set; } //ICommand cua datagrid
        
        private ICommand commandFuntion;
        public ICommand CommandFuntion
        {
            get
            {
                return this.commandFuntion;
            }
            set
            {
                this.commandFuntion = value;
                this.OnPropertyChanged("CommandFuntion");
            }
        }
        private NHANVIEN currentNhanVien;
        public NHANVIEN CurrentNhanVien
        {
            get
            {
                return this.currentNhanVien;
            }
            set
            {
                this.currentNhanVien = value;
                this.newNhanVien = this.CurrentNhanVien;
                if (this.currentNhanVien == null)
                {
                    this.newNhanVien = new NHANVIEN();
                    
                }
                else this.RefreshToViewErrors();
                this.OnPropertyChanged("CurrentNhanVien");
            }
        }
        public ObservableCollection<BACLUONG> BacLuongData { get; set; }
        public ObservableCollection<CHUCVU> ChucVuData { get; set; }
        public ObservableCollection<LOAITAIKHOAN> LoaiTaiKhoanData { get; set; }
        public ObservableCollection<PHONGBAN> PhongBanData { get; set; } 
        #endregion
        #region funtion
        public NhanVienViewModel()
        {
            //MessageBox.Show("Called");
            this.CommandFuntion = new DelegateCommand<object>(Funtion);
            CommandDatagrid = new DelegateCommand<object>(ActionDatagrid);
          
            this.GetbyAllNhanVien();
            this.GetdataCombox();
            this.nhanvienclient.InsertNhanVienCompleted += nhanvienclient_InsertNhanVienCompleted;
            this.nhanvienclient.UpdateNhanVienCompleted += nhanvienclient_UpdateNhanVienCompleted;
            this.nhanvienclient.DeleteNhanVienCompleted += nhanvienclient_DeleteNhanVienCompleted;
        }

        void nhanvienclient_DeleteNhanVienCompleted(object sender, DeleteNhanVienCompletedEventArgs e)
        {
            MessageBox.Show(e.Result);
            this.GetbyAllNhanVien();
        }

        void nhanvienclient_UpdateNhanVienCompleted(object sender, UpdateNhanVienCompletedEventArgs e)
        {
            MessageBox.Show(e.Result);
            this.GetbyAllNhanVien();
        }

        void nhanvienclient_InsertNhanVienCompleted(object sender, InsertNhanVienCompletedEventArgs e)
        {
            MessageBox.Show(e.Result);
            this.GetbyAllNhanVien();
        }

       

        private void ActionDatagrid(object obj)
        {
            if (obj.ToString() == "Insert")
            {
                var window = new ChildForm();
                window.Title = "NhanVienEdit";
                window.Show();
            }
            if (obj.ToString() == "Delete")
            {
                MessageBox.Show("Delete");
            }
        }
        private void Funtion(object parameter)
        {           
            if (parameter.ToString() == "Insert")
            {
                    
                    this.nhanvienclient.InsertNhanVienAsync(this.CurrentNhanVien);
               
               
            } //ham insert
            if (parameter.ToString() == "Update") 
            {
                
                this.nhanvienclient.UpdateNhanVienAsync(this.CurrentNhanVien);
            } //ham update
            if (parameter.ToString() == "Delete") {
                
             
                this.nhanvienclient.DeleteNhanVienAsync(this.CurrentNhanVien.MaNhanVien);
            } //ham refresh
            if (parameter.ToString() == "Upload")
            {
                try
                {

                    OpenFileDialog dialog = new OpenFileDialog();
                    bool? diaglogresult = dialog.ShowDialog();
                    FileInfo fs = dialog.File;
                    if (diaglogresult.Value)
                    {
                        Stream filestream = dialog.File.OpenRead();

                        BitmapImage aaaaa = new BitmapImage();
                        aaaaa.SetSource(filestream);
                        this.CurrentNhanVien.Anh = new Binary { Bytes = ReadFully(filestream) };
                                              
                     }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
        //chuyen doi qua kieu byte[]
        public static byte[] ReadFully(Stream input)
        {
            MemoryStream ms = new MemoryStream();
            input.Position = 0;
            input.CopyTo(ms);
            return ms.ToArray();

        }

        private void GetdataCombox()
        {
            this.bacluongclient.GetByAllBacLuongCompleted += (s, e) =>
            {
                try
                {
                    if (e.Result != null)
                        this.BacLuongData = e.Result;
                    else
                        this.BacLuongData = new ObservableCollection<BACLUONG>();
                    this.OnPropertyChanged("BacLuongData");
                }
                catch (Exception)
                {
                }
            };
            this.bacluongclient.GetByAllBacLuongAsync();

            this.chucvuclient.GetByAllChucVuCompleted += (s, e) =>
            {
                try
                {
                    if (e.Result != null)
                        this.ChucVuData = e.Result;
                    else
                        this.ChucVuData = new ObservableCollection<CHUCVU>();
                    this.OnPropertyChanged("ChucVuData");
                }
                catch (Exception)
                {
                }
            };
            this.chucvuclient.GetByAllChucVuAsync();

            this.loaitaikhoanclient.GetByAllLoaiTaiKhoanCompleted += (s, e) =>
            {
                try
                {
                    if (e.Result != null)
                        this.LoaiTaiKhoanData = e.Result;
                    else
                        this.LoaiTaiKhoanData = new ObservableCollection<LOAITAIKHOAN>();
                    this.OnPropertyChanged("LoaiTaiKhoanData");
                }
                catch (Exception)
                {
                }
            };
            this.loaitaikhoanclient.GetByAllLoaiTaiKhoanAsync();

            this.phongbanclient.GetByAllPhongBanCompleted += (s, e) =>
            {
                try
                {
                    if (e.Result != null)
                        this.PhongBanData = e.Result;
                    else
                        this.PhongBanData = new ObservableCollection<PHONGBAN>();
                    this.OnPropertyChanged("PhongBanData");
                }
                catch (Exception)
                {
                }
            };
            this.phongbanclient.GetByAllPhongBanAsync();

        }
        private void GetbyAllNhanVien()
        {
            // MessageBox.Show("Called");
            this.nhanvienclient.GetByAllNhanVienCompleted += (s, e) =>
            {
               // MessageBox.Show(e.Result[0].MaNhanVien.ToString());
               
                collection = new PagedCollectionView(e.Result);
                OnPropertyChanged("collection");
            };
            this.nhanvienclient.GetByAllNhanVienAsync();
        }      
       
        

        private void RefreshToViewErrors()
        {
            //this.newNhanVien.MaNhanVien = this.CurrentNhanVien.MaNhanVien;
            this.newNhanVien.HoTen = this.CurrentNhanVien.HoTen;
            this.newNhanVien.CMND = this.CurrentNhanVien.CMND;
            this.newNhanVien.NgaySinh = this.CurrentNhanVien.NgaySinh;
            this.newNhanVien.NgayKiHopDong = this.CurrentNhanVien.NgayKiHopDong;
            this.newNhanVien.SoHopDong = this.CurrentNhanVien.SoHopDong;
            this.newNhanVien.MaBacLuong = this.CurrentNhanVien.MaBacLuong;
            this.newNhanVien.MaChucVu = this.CurrentNhanVien.MaChucVu;
            this.newNhanVien.MaLoaiTaiKhoan = this.CurrentNhanVien.MaLoaiTaiKhoan;
            this.newNhanVien.MaPhongBan = this.CurrentNhanVien.MaPhongBan;
            this.newNhanVien.MaChucVu = this.CurrentNhanVien.MaChucVu;
          


        }
        #endregion

    }
}
